﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_NhanVien : System.Web.UI.Page
{
    TiemPhongDataContext db = new TiemPhongDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadDataListView();
            loadCB();

        }

    }
    protected void btnThem_Click1(object sender, EventArgs e)
    {
        try
        {
            NhanVien nv = new NhanVien();
            nv.MaNV = txtMNV.Text;
            nv.TenNV = txtTNV.Text;
            nv.DiaChi = txtDiaChi.Text;
            nv.NgaySinh = DateTime.ParseExact(this.txtNgaySinh.Text, "dd/MM/yyyy", null);
            nv.GioiTinh = GioiTinh.Value;
            //nv.ChucVu = txtChucVu.Text;
            db.NhanViens.InsertOnSubmit(nv);
            db.SubmitChanges();
            messageID.Enabled = true;
            messageID.Text = "Thêm nhân viên thành công !";
            loadDataListView();
            ClearControl();
        }
        catch (Exception m)
        {
            messageID.Enabled = true;
            messageID.Text = "Them that bai!" + m.Message;

        }
    }
    protected void btnSua_Click1(object sender, EventArgs e)
    {

    }
    protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton selectedRadioButton = (RadioButton)sender;
        GioiTinh.Value = selectedRadioButton.Text;
    }
    protected void LB_Click(object sender, EventArgs e)
    {

    }
    protected void loadCB()
    {
        //var cbList = db.ChucVus.Where(x => x.Level > 0).ToList();

        //cbChucVu.DataValueField = "ChucVuId";
        //cbChucVu.DataTextField = "Name";
        //cbChucVu.DataSource = cbList;
        //cbChucVu.DataBind();
        //cbChucVu.Items.Insert(0, "--Select--");
    }
    protected void loadDataListView()
    {
        var list = db.NhanViens.ToList();
        this.ListViewNhanVien.DataSource = list;
        this.ListViewNhanVien.DataBind();
        txtMNV.Enabled = false;
        txtMNV.Text =loadMaNV();
    }
    protected string loadMaNV()
    {
        string countID = "";
        var selectID = (from u in db.NhanViens select u.MaNV).Count() + 1;
        countID = selectID.ToString();
        countID = "NV" + countID;
        return countID;
    }    
    protected void ClearControl()
    {
        txtDiaChi.Text = "";
        txtMNV.Text = "";
        txtNgaySinh.Text = "";
        txtTNV.Text = "";
        rbNam.Checked = false;
        rbNu.Checked = false;
        loadCB();
    }
    protected void ListViewNhanVien_OnItemCommand(object sender, ListViewCommandEventArgs e)
    {
        var idNhanVien = e.CommandArgument.ToString();
        var ResultNV = db.NhanViens.FirstOrDefault(v => v.MaNV.Equals(idNhanVien));
        if (e.CommandName == "Delete")
        //{
        //    ResultNV.Status = -1;
        //    db.SubmitChanges();
        //    ClearControl();
        //    loadListView();
        //}
        if (e.CommandName == "Update")
        {
            txtMNV.Text = ResultNV.MaNV;
            txtTNV.Text = ResultNV.TenNV;
            txtNgaySinh.Text = ResultNV.NgaySinh.HasValue ? Convert.ToDateTime(ResultNV.NgaySinh).ToString("d") : String.Empty;
            txtDiaChi.Text = ResultNV.DiaChi;
            //txtChucVu.Text = ResultNV.TenChucVu;
            GioiTinh.Value = ResultNV.GioiTinh;
            btnSua.Enabled = true;
            btnThem.Enabled = false;
        }
    }
    protected void ListViewNhanVien_OnItemDeleting(object sender, ListViewDeleteEventArgs e) { }
    protected void ListViewNhanVien_OnItemUpdating(object sender, ListViewUpdateEventArgs e) { }
}